<?php

namespace Admin\Controller;
use Think\Controller;

class ApprecommendController extends Controller{

    //列表

     public function lists(){

        $this->isAjaxLogin();//检查是否登录

        $page = I('post.page/d');
        $page_size = I('post.page_size/d');
      
        $statustype = I('post.statustype');
   



        $model2 = M('gametype');
        $model1 = M('gameinfo');

        if($statustype==5){

            $count = $model1->join('table_gametype  on table_gametype.gameid=table_gameinfo.id')->where('table_gametype.isgame=1 || table_gametype.isbank=1 ||  table_gametype.isapply=1 ')->count();
            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
     
            $result = $model1->join('table_gametype  on table_gametype.gameid=table_gameinfo.id')->order('table_gametype.gameid desc')->where('table_gametype.isgame=1 || table_gametype.isbank=1 ||  table_gametype.isapply=1 ')->limit($limitStart,$page_size)->select();
            $data['sele'] = 1;

    }


    if($statustype=="0"){
            $count = $model2->where('isapply=1')->count();

            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 2;
            $result = $model1->join('table_gametype ON table_gametype.gameid=table_gameinfo.id')->where('table_gametype.isapply=1')->order('table_gametype.applyrank desc')->limit($limitStart,$page_size)->select();
        }elseif($statustype=="1"){
            $count = $model2->where('isbank=1')->count();
            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 2;
            $result = $model1->join('table_gametype ON table_gametype.gameid=table_gameinfo.id')->where('table_gametype.isbank=1')->order('table_gametype.bankingrank desc')->limit($limitStart,$page_size)->select();
        }elseif($statustype=="2"){
            $count = $model2->where('isgame=1')->count();
            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 2;
            $result = $model1->join('table_gametype ON table_gametype.gameid=table_gameinfo.id')->where('table_gametype.isgame=1')->order('table_gametype.gamerank desc')->limit($limitStart,$page_size)->select();
        }elseif ($statustype=="4") {
            $count = $model2->where('table_gametype.isgame=1 || table_gametype.isbank=1 ||  table_gametype.isapply=1')->count();
            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 1;
            $result = $model1->join('table_gametype ON table_gametype.gameid=table_gameinfo.id')->where('table_gametype.isgame=1 || table_gametype.isbank=1 ||  table_gametype.isapply=1')->order('table_gametype.gameid desc')->limit($limitStart,$page_size)->select();
        }


        if($result !== false){

            $data['page']['total_page'] = $total;
            $data['list'] = $result;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }

	
	/*
					**************************游戏页面app相关方法 *****************************
	*/

	/*游戏平台分类渲染*/
	public  function  gamexuanran(){
		 //$this->isAjaxLogin();
        $result = array
                      (
                        array(
                             'id'=>'0','name'=>'应用'
                          ),
                        array('id'=>'1','name'=>'金融'
                        ),
                        array(
                            'id'=>'2','name'=>'手机游戏游戏'
                        )
                      );

        if($result){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;

        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "查询数据失败！";
        }

        $this->ajaxReturn($ajaxReturn);
	}				




   



	/*游戏上下架*/

	public function isshow(){

		$isshow =I('post.isshow');//1上架，0下架
		$id = I('post.id');

		$data['isshow']=$isshow;

		$game = M('gameinfo');

		$result = $game->where('id='.$id)->save($data);

		if($result!=false){

			$ajaxReturn['code'] = 1;
			$ajaxReturn['msg'] = '添加成功！';
			$ajaxReturn['data'] = $result;

		}else{

			$ajaxReturn['code'] = 2;
			$ajaxReturn['msg'] = "添加失败！";

		}

		$this->ajaxReturn($ajaxReturn);
	}

	/*游戏页面的列表展示*/

	public function gamelist(){
        $page =I('post.page/d');
        $page_size = I('post.page_size/d');
		$statustype=I('post.statustype');
		$model1=M('gameinfo');
		$model2=M('gametype');

		if($statustype=="0"){
            $count = $model2->where('isapply=1')->count();

            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 2;
            $result = $model1->join('table_gametype ON table_gametype.gameid=table_gameinfo.id')->where('table_gametype.isapply=1')->order('table_gametype.applyrank desc')->limit($limitStart,$page_size)->select();
        }elseif($statustype=="1"){
            $count = $model2->where('isbank=1')->count();
            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 2;
            $result = $model1->join('table_gametype ON table_gametype.gameid=table_gameinfo.id')->where('table_gametype.isbank=1')->order('table_gametype.bankingrank desc')->limit($limitStart,$page_size)->select();
        }elseif($statustype=="2"){
            $count = $model2->where('isgame=1')->count();
            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 2;
            $result = $model1->join('table_gametype ON table_gametype.gameid=table_gameinfo.id')->where('table_gametype.isgame=1')->order('table_gametype.gamerank desc')->limit($limitStart,$page_size)->select();
        }elseif ($statustype=="4") {
            $count = $model2->where('table_gametype.isgame=1 || table_gametype.isbank=1 ||  table_gametype.isapply=1')->count();
            $total =  ceil($count / $page_size);
            $limitStart = ($page-1) * $page_size;
            $data['sele'] = 1;
            $result = $model1->join('table_gametype ON table_gametype.gameid=table_gameinfo.id')->where('table_gametype.isgame=1 || table_gametype.isbank=1 ||  table_gametype.isapply=1')->order('table_gametype.gameid desc')->limit($limitStart,$page_size)->select();
        }

        if($result !== false){

            $data['page']['total_page'] = $total;
            $data['list'] = $result;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }

        $this->ajaxReturn($ajaxReturn);
	}

	/*
     * 排序
     */
    public function gamerank (){

        $apptype=I('post.statustype');

        $thisid = I('post.id');
        $type   = I('post.type');
        $and    = $type == 1 ? '>' : '<';
        $order  = $type == 1 ? 'asc' : 'desc';

        $ArticleModel = M('gametype');



        if($apptype=="0"){
            $thisinfo   = $ArticleModel->where('gameid='.$thisid)->getField('applyrank,ishotgame');
            $thisrank   = array_keys($thisinfo)[0];
            $thiscid    = array_values($thisinfo)[0];


            $nextinfo   = $ArticleModel->where('applyrank'.$and.$thisrank)->order('applyrank '.$order)->getField('gameid,applyrank');
            $nextid     = array_keys($nextinfo)[0];
            $nextrank   = array_values($nextinfo)[0];



            if($thisrank && $nextid && $nextrank){

                $ArticleModel-> where('gameid='.$thisid)->setField('applyrank',$nextrank);
                $ArticleModel-> where('gameid='.$nextid)->setField('applyrank',$thisrank);

                $data['thisid'] = $thisid;
                $data['thisrank'] = $thisrank;
                $data['nextid'] = $nextid;
                $data['nextrank'] = $nextrank;

                $ajaxReturn['code'] = 1;
                $ajaxReturn['msg'] = "SUCCESS";
                $ajaxReturn['data'] = $data;
            }else{

                $ajaxReturn['code'] = 2;
                $ajaxReturn['msg'] = "数据更新失败！";
            }
        }elseif ($apptype=="1") {
            $thisinfo   = $ArticleModel->where('gameid='.$thisid)->getField('bankingrank,ishotgame');
            $thisrank   = array_keys($thisinfo)[0];
            $thiscid    = array_values($thisinfo)[0];


            $nextinfo   = $ArticleModel->where('bankingrank'.$and.$thisrank)->order('bankingrank '.$order)->getField('gameid,bankingrank');
            $nextid     = array_keys($nextinfo)[0];
            $nextrank   = array_values($nextinfo)[0];

            if($thisrank && $nextid && $nextrank){

                $ArticleModel-> where('gameid='.$thisid)->setField('bankingrank',$nextrank);
                $ArticleModel-> where('gameid='.$nextid)->setField('bankingrank',$thisrank);

                $data['thisid'] = $thisid;
                $data['thisrank'] = $thisrank;
                $data['nextid'] = $nextid;
                $data['nextrank'] = $nextrank;

                $ajaxReturn['code'] = 1;
                $ajaxReturn['msg'] = "SUCCESS";
                $ajaxReturn['data'] = $data;
            }else{

                $ajaxReturn['code'] = 2;
                $ajaxReturn['msg'] = "数据更新失败！";
            }
        }elseif ($apptype=="2") {
            $thisinfo   = $ArticleModel->where('gameid='.$thisid)->getField('gamerank,ishotgame');
            $thisrank   = array_keys($thisinfo)[0];
            $thiscid    = array_values($thisinfo)[0];


            $nextinfo   = $ArticleModel->where('gamerank'.$and.$thisrank)->order('gamerank '.$order)->getField('gameid,gamerank');
            $nextid     = array_keys($nextinfo)[0];
            $nextrank   = array_values($nextinfo)[0];

            if($thisrank && $nextid && $nextrank){

                $ArticleModel-> where('gameid='.$thisid)->setField('gamerank',$nextrank);
                $ArticleModel-> where('gameid='.$nextid)->setField('gamerank',$thisrank);

                $data['thisid'] = $thisid;
                $data['thisrank'] = $thisrank;
                $data['nextid'] = $nextid;
                $data['nextrank'] = $nextrank;

                $ajaxReturn['code'] = 1;
                $ajaxReturn['msg'] = "SUCCESS";
                $ajaxReturn['data'] = $data;
            }else{

                $ajaxReturn['code'] = 2;
                $ajaxReturn['msg'] = "数据更新失败！";
            }
        }
        

        $this->ajaxReturn($ajaxReturn);

    }

     public  function  gamedelete(){
        $id=I('post.id');
        $model=M('gameinfo');
        $model1=M('gametype');


        $model->where('id='.$id)->delete();
        $model1->where('gameid='.$id)->delete();

        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg']  = "SUCCESS";

        $this->ajaxReturn($ajaxReturn);
    }
}